﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Threading;
using System.ComponentModel;
using System.IO;

namespace NET.Library.Comm
{
    /// <summary>
    /// 
    /// </summary>
    public class AsyncEmail : Email
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ToEmail"></param>
        /// <param name="FromEmail"></param>
        /// <param name="CCTo"></param>
        /// <param name="BCCTo"></param>
        /// <param name="MessageSubject"></param>
        /// <param name="MessageBody"></param>
        /// <param name="IsBodyHtml"></param>
        /// <param name="MailPriority"></param>
        /// <param name="Attachment"></param>
        /// <returns></returns>
        public static new bool MailSender(ref MailAddress ToEmail, MailAddress FromEmail, MailAddress CCTo,
            MailAddress BCCTo, string MessageSubject, string MessageBody, bool IsBodyHtml,
            MailPriority MailPriority, FileInfo Attachment)
        {
            MailMessage mailMsg = new MailMessage();
            try
            {
                mailMsg.To.Add(ToEmail);
                mailMsg.From = FromEmail;
                    if (CCTo!=null)
                    {
                        mailMsg.CC.Add(CCTo);
                    }
                    if (BCCTo != null)
                    {
                        mailMsg.Bcc.Add(BCCTo);
                    }
                    mailMsg.Subject = MessageSubject;
                    mailMsg.Body = MessageBody;

                    mailMsg.IsBodyHtml = IsBodyHtml;
                    
                    if (MailPriority.ToString() == "")
                    {
                        MailPriority = MailPriority.Normal;
                    }
                    mailMsg.Priority = MailPriority;
                    //if (Attachment.Length > 0)
                    if (Attachment != null)
                    {
                        if (Attachment.Exists)
                        {
                            mailMsg.Attachments.Add(new Attachment(Attachment.FullName));
                            //cdoMsg.Attachments.Add(new MailAttachment(Attachment.FullName));
                        }
                        //else log file error?
                    }
                    //SmtpMail.SmtpServer = SmtpServer;

                    SmtpClient client = new SmtpClient();
                    if (!string.IsNullOrEmpty(SmtpServer))
                    {
                        client.Host = SmtpServer;
                    }

                    client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
                    // The userState can be any object that allows your callback 
                    // method to identify this send operation.
                    // For this example, the userToken is a string constant.
                    string userState = "Async Email Message";
                    client.SendAsync(mailMsg, userState);
                    
                    return true;
            }
            finally
            {
                mailMsg.Dispose();
                //strInvalidRecipients = null;
            }
        }

        static bool mailSent = false;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
            String token = (string)e.UserState;
            //string returnMsg = "";

            if (e.Cancelled)
            {
               // returnMsg= "Send canceled";
            }
            if (e.Error != null)
            {
                //returnMsg= e.Error.ToString();
            }
            else
            {
               // returnMsg= "Message sent";
            }
            mailSent = true;
           // return returnMsg;
        }

        

    }
}
